package com.hh.legalStoreRoom.controller;

import com.hh.legalStoreRoom.VO.*;
import com.hh.legalStoreRoom.service.LegalStoreRoomService;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Hanh
 * @date 2023/8/16 15:57s
 */
@RestController
@RequestMapping("legalStoreRoom")
@CrossOrigin
public class LegalStoreRoomController {

    @Autowired
    private LegalStoreRoomService legalStoreRoomService;


    /**
     * 分页查询
     * @param queryStorePageVo
     * @return
     */
    @PostMapping("queryPage")
    private ResultVo queryLegalStoreRoomPage(@RequestBody QueryLegalStoreRoomPageVo queryStorePageVo){
        return legalStoreRoomService.queryStorePage(queryStorePageVo);
    }


    /**
     * 根据法人code获取到对应的库房库区库位
     * @param legalPersonCode
     * @return
     */
    @GetMapping("getStore")
    private ResultVo queryLegalStoreAll(@RequestParam("legalPersonCode")String legalPersonCode){
        return legalStoreRoomService.queryLegalStoreAll(legalPersonCode);
    }

    /**
     * 根据库房codeList与属性code获取对应的属性信息
     * @param queryAreaVo
     * @param bindingResult
     * @return
     */
    @PostMapping("getStoreArea")
    private ResultVo queryAreaByRoomListAndAttribute(@RequestBody @Valid QueryAreaVo queryAreaVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }else {
            return legalStoreRoomService.queryAreaByRoomListAndAttribute(queryAreaVo);
        }
    }

    /**
     * 根据库区codeList查询对应的库位信息
     * @param queryStoreVo
     * @param bindingResult
     * @return
     */
    @PostMapping("getStore")
    private ResultVo queryStoreByAreaCodes(@RequestBody @Valid QueryStoreVo queryStoreVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }else {
            return legalStoreRoomService.queryStoreByAreaCodes(queryStoreVo);
        }
    }

    /**
     * 新增库位中的法人信息,修改接口同样访问这个
     * @param addLegalStoreVo
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    private ResultVo addLegalStoreRoom(@RequestBody AddLegalStoreVo addLegalStoreVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalStoreRoomService.addLegalStoreRoom(addLegalStoreVo);
    }

    /**
     * 基于原有功能进行添加功能优化
     * @param addLegalStoreVo
     * @param bindingResult
     * @return
     */
    @PostMapping("add2")
    private ResultVo addLegalStoreRoom2(@RequestBody AddLegalStoreVo addLegalStoreVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalStoreRoomService.addLegalStoreRoom2(addLegalStoreVo);
    }


    @PostMapping("updateStatus")
    private ResultVo updateStatus(@RequestBody UpdateStatusVo updateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalStoreRoomService.updateStatus(updateStatusVo);
    }


    /**
     * 做一个接口分离，只应用于该页面的库房查询及根据库区属性查询
     * @param queryStoreRoomVo
     * @return
     */
    @PostMapping("queryStoreRoom")
    private ResultVo queryStoreRoom(@RequestBody QueryStoreRoomVo queryStoreRoomVo){
        return legalStoreRoomService.queryStoreRoom(queryStoreRoomVo);
    }
}
